// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Practices.Unity.InterceptionExtension;
using ValidationAspects.Sdk.Interception;

namespace ValidationAspects.Unity
{
	public class ValidationMatchingRule : IMatchingRule
	{
		public bool AttributedPropertiesOnly { get; set; }
		public bool AttributedMethodsOnly { get; set; }

		#region Implementation of IMatchingRule
		public bool Matches(MethodBase member)
		{
			if (member.DeclaringType.IsAbstract)
				return false;

			IEnumerable<MethodInfo> validatableSetters = GetInterceptionValidationProperties.ForType(member.DeclaringType, AttributedPropertiesOnly).Select(p => p.GetSetMethod());
			IEnumerable<MethodInfo> validatableMethods = GetInterceptionValidationMethods.ForType(member.DeclaringType, AttributedMethodsOnly);

			return validatableSetters.Concat(validatableMethods).OfType<MethodBase>().Contains(member);
		}
		#endregion
	}
}